<template>

  <div class="L-base-list"  v-loading="isLoading">
    <!-- <van-sticky> -->
    <div class="L-base-search">
     <el-input v-model="keyword" style="max-width: 600px" placeholder="请输入关键字">
        <template #append ><span @click="handleSearch" style="cursor: pointer;">搜索</span> </template>
      </el-input>
    </div>

    <!-- 列表 -->
    <div v-if="list.length>0" style="flex-grow: 1;flex-shrink: 1;height: 90vh;overflow: scroll;" >
      <div class="base-item" v-for="(item, index) in list" :key="index">
        <RefItem :item="item" :mode="refMode"  @click="handleClick(item)"></RefItem>
      </div>
    </div>

    <el-empty v-else description="没有数据" />

  </div>
  <!-- <van-tabbar safe-area-inset-bottom v-if="refMode === 'multi'"> -->
  <!-- <div class="L-orgRef-tabbar"> -->
  <!-- <van-button color="#1989fb" type="primary" style="width:90%" @click="handleConfirm">确定</van-button> -->
  <!-- </div> -->
  <!-- </van-tabbar> -->

</template>

<script setup lang="ts">
// import { closeToast, showLoadingToast } from "vant";

// import BaseSearch from "../BaseSearch.vue";
import RefItem from "./RefItem.vue";
import { apiConfigurable } from "@/request/api";
import { useRouter, useRoute } from "vue-router";
import { onMounted, ref, defineProps, } from "vue";
const router = useRouter();
const route = useRoute();

const list = ref([]);
const page = ref(1);
const pagecount = ref(50); //每页条数
const isLoading = ref(false); //是否加载中
const finished = ref(false)

const keyword = ref("");


const selecteddata = ref([]);
function handleConfirm() {
  let id = "";
  let title = "";
  let address = "";
  let idaccount_ref = ""

  for (let i = 0; i < selecteddata.value.length; i++) {
    const item = selecteddata.value[i];
    id += `${item.id};`;
    title += `${item.sname};`;
    address += `${item?.saddress};`;
    idaccount_ref += `${item?.idaccount_ref};`

  }
  const returnObj = {
    title,
    id,
    address,
    idaccount_ref
  };
  props.handleRefClick(returnObj);
}
onMounted(() => {

  getRefData()
})

const props = defineProps({
  refParams: {
    type: Object,
    default: {},
  },
  handleRefClick: {
    type: Function,
    default: () => { },
  },
  handleRefCancle: {
    type: Function,
    default: () => { },
  },
  refMode: {
    type: String,
    default: "normal",
  },

});

function getRefData() {
  const { refParams } = props

  let data = {
    ref_object: refParams.object,
    ref_toobject: refParams.toobject,
    ref_tofieldas: refParams.field,
    ref_inview: refParams.inview || "",
    view_page: page.value,
    view_id: refParams.sRefKey,
    ref_classname: refParams.classname,
    ref_params: refParams.params,
    ref_conditionval: keyword.value,
  };
  const url = `/capi/ref.listnew.do`;
  isLoading.value = true;
  apiConfigurable(url, data, "post").then(res => {
    if (res.status === 200) {
      list.value = res?.data?.jsondata?.outdata
    }
  }).finally(() => {
    isLoading.value = false;
  })

}



//字段映射hooks

interface returnObjInterface {
  id: string,
  title: string,
  [key: string]: string

}
async function handleClick(item) {

  const { refMode, refParams, handleRefClick } = props
  if (refMode === "normal") {
    const returnObj: returnObjInterface = {
      id: item.id,
      title: item.sname,
    };


    props.handleRefClick(returnObj);
  }
}

//初始化查询条件
function initCondition() {
  page.value = 1;
  finished.value = false;
  list.value = [];
}

function handleSearch() {
  initCondition();
  getRefData();
}
</script>

<style scoped></style>